/******************************************************************************
* File description: RunningTemperatureTable.qml
* Function: 实验运行：温度表格
* Current Version: 1.0.0
* Author: 张宇轩
* Date: 2024-08-27
*  -----
*  Copyright by 博坤生物 .
 *****************************************************************************/
import QtQuick 2.14
import QtQuick.Controls 2.14
import QtQuick.Layouts 1.14
import Qt.labs.qmlmodels 1.0

Item {
    id: root

    /* property 定义开始 */
    property var tableModel: []

    // 私有属性
    property var _horHeaderData: ["卡位", "温度℃"]
    property int _horHeaderHeight: 43
    property int _tableItemHeight: 47
    property var _tableModel: root.tableModel
    /* property 定义结束 */

    // 表格
    Item {
        id: tableContainer
        anchors.fill: parent

        // 表格部分
        TableView {
            id: tableContent
            width: parent.width
            height: parent.height - _horHeaderHeight
            anchors {
                top: parent.top
                topMargin: _horHeaderHeight
            }
            clip: true
            boundsBehavior: Flickable.StopAtBounds
            columnSpacing: 0
            rowSpacing: 0

            columnWidthProvider: function(column){
                return horizontalHeader.width / _horHeaderData.length
            }

            rowHeightProvider: function(row){
                return _tableItemHeight
            }

            model: TableModel {
                id: tableModelCon

                TableModelColumn {display: "卡位"}
                TableModelColumn {display: "温度"}

                rows: _tableModel
            }

            delegate: Item {
                width: tableContent.columnWidthProvider(column)
                height: tableContent.rowHeightProvider(row)

                Label {
                    anchors.centerIn: parent
                    text: display
                    font.pixelSize: 16
                    color: "#FF222222"
                }

                // 下边框
                Canvas {
                    anchors.fill: parent
                    antialiasing: true
                    onPaint: {
                        var ctx = getContext("2d")
                        ctx.clearRect(0, 0, width, height)
                        ctx.strokeStyle = "#FF999999"
                        ctx.lineWidth = 2
                        ctx.beginPath()
                        // 下边框
                        ctx.moveTo(0, height)
                        ctx.lineTo(width, height)
                        ctx.stroke()
                    }
                }
            }

            // 事件
            onWidthChanged: {
                tableContent.forceLayout()
            }

            onHeightChanged: {
                tableContent.forceLayout()
            }
        }

        // 表头部分
        // 横向表头
        Item {
            id: horizontalHeader
            width: parent.width
            height: _horHeaderHeight
            anchors {
                top: parent.top
                left: parent.left
                right: parent.right
            }

            Row {
                id: horHeaderRowLayout
                spacing: 0

                Repeater {
                    id: horHeaderItemRepeater
                    model: _horHeaderData

                    Item {
                        id: horheaderItem
                        width: horizontalHeader.width / _horHeaderData.length
                        height: horizontalHeader.height

                        Rectangle {
                            id: horHeaderBg
                            anchors.fill: parent
                            border.width: 0
                            color: "transparent"

                            Text {
                                id: horHeaderText
                                text: qsTr(modelData)
                                anchors.centerIn: parent
                                font.pixelSize: 16
                                color: "#FF222222"
                            }
                        }

                        // 画边框
                        Canvas {
                            id: horHeaderCanvas
                            antialiasing: true
                            anchors.fill: parent
                            onPaint: {
                                var ctx = getContext("2d")
                                ctx.clearRect(0, 0, width, height)
                                ctx.strokeStyle = "#FF999999"
                                ctx.lineWidth = 2
                                ctx.beginPath()

                                // 上边框
                                ctx.moveTo(0, 0)
                                ctx.lineTo(width, 0)
                                ctx.stroke()

                                // 下边框
                                ctx.moveTo(0, height)
                                ctx.lineTo(width, height)
                                ctx.stroke()
                            }
                        }
                    }
                }
            }
        }
    }
}
